home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Utilities / BackClock / sources / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-11  |  10.0 KB  |  312 lines

  1. /*****************************************************************************
  2.  * 
  3.  * Nom                          : utils.c
  4.  * desc                         : utilitaires projet
  5.  *
  6.  * version                      : $VER: utils.c 2.2 (11.04.98)
  7.  *
  8.  * ver 2.1: error handling
  9.  * ver 2.2: some bug fixed
  10.  *
  11.  *****************************************************************************
  12.  */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <math.h>
  17. #include <exec/exec.h>
  18. #include <exec/ports.h>
  19. #include <exec/io.h>
  20. #include <exec/tasks.h>
  21. #include <exec/libraries.h>
  22. #include <dos/datetime.h>
  23. #include <dos/dosextens.h>
  24. #include <workbench/workbench.h>
  25. #include <libraries/notifyintuition.h>
  26.  
  27. #include <proto/exec.h>
  28. #include <proto/intuition.h>
  29. #include <proto/dos.h>
  30. #include <proto/graphics.h>
  31. #include <proto/gadtools.h>
  32. #include <proto/icon.h>
  33. #include "utils.h"
  34. #include "timer.h"
  35. #include "tracewin.h"
  36. #include "notify.h"
  37. #include "partial.h"
  38. #include "conf.h"
  39. #include "obp.h"
  40.  
  41. struct Library * NotifyIntuitionBase ;
  42.  
  43. char * ptrTime = NULL ;
  44. struct DateTime * dt = NULL ;
  45.  
  46. idWin * init(WBArg * appName) {
  47.   /* initalize the main window
  48.    * and the project
  49.    */
  50.   struct Screen  * WBScreen = NULL ;                 // lock on WB
  51.   idWin * prjWin = NULL ;                            // main prj
  52.   
  53.   if ((prjWin = AllocVec(sizeof(idWin), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  54.     return(NULL) ;
  55.     
  56.     
  57.   if ((prjWin->date = AllocVec(sizeof(struct DateStamp), MEMF_PUBLIC)) == NULL) {
  58.     close(prjWin) ;
  59.     return(NULL) ;
  60.   }
  61.     
  62.   if ((prjWin->Notify = AllocVec(sizeof(struct IntNotifyRequest), MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {
  63.     close(prjWin) ;
  64.     return(NULL) ;
  65.   }
  66.   
  67.   
  68.   if ((NotifyIntuitionBase = OpenLibrary(NOTIFYINTUITIONNAME, NOTIFYINTUITION_VMIN)) != NULL) {
  69.         /* open the window
  70.          */
  71.     LoadConf(prjWin) ;
  72.     WBScreen = LockPubScreen("Workbench") ;
  73.     top = WBScreen->BarHeight ; 
  74.     prjWin->wb   = WBScreen ;
  75.     init_bitmap(prjWin) ;
  76.     prjWin->win = OpenWindowTags(NULL, WA_Left,   prjWin->backWin.posX,
  77.                                        WA_Top,         prjWin->backWin.posY,
  78.                                        WA_Width,       prjWin->backWin.width,
  79.                                        WA_Height,      prjWin->backWin.height,
  80.                                        WA_IDCMP,       IDCMP,
  81.                                        WA_MinWidth,     50,
  82.                                        WA_MinHeight,   50,
  83.                                        WA_MaxHeight,   200,
  84.                                        WA_MaxWidth,    200,
  85.                                        WA_Flags,       WFLG_BORDERLESS,
  86.                                        WA_ScreenTitle, "BackClock", 
  87.                                        WA_NewLookMenus, TRUE,
  88.                                        WA_PubScreenName, "Workbench", TAG_DONE) ;
  89.     
  90.     szgdg.Height = top - 1 ;
  91.     szgdg.NextGadget = &tagdg ;
  92.     tagdg.LeftEdge  = prjWin->win->Width - 10 ;
  93.     tagdg.TopEdge   = prjWin->win->Height - 10 ;
  94.     tagdg.Width     = 10 ;
  95.     tagdg.Height    = 10 ;
  96.   
  97.     AddGList(prjWin->win, &szgdg, 0, 2, NULL) ;
  98.    
  99.     RefreshGList(&szgdg, prjWin->win, NULL, -1) ;
  100.                                                                                              
  101.     if (prjWin->win == NULL)
  102.       return(NULL) ;
  103.     
  104.     if (prjWin->win)
  105.       WindowToBack(prjWin->win) ;
  106.     if (prjWin->wb)
  107.       UnlockPubScreen(NULL, WBScreen) ;
  108.     
  109.                                                                                                 
  110.           // initialisation ...  (fenetre...)
  111.   }else {
  112.     close(prjWin) ;
  113.     return(NULL) ;
  114.   }
  115.   initwin(prjWin) ;
  116.   if (!initTimer(prjWin))   { close(prjWin) ; return(NULL) ;}
  117.   if (!startNotify(prjWin)) { close(prjWin) ; return(NULL) ;}
  118.   if ((ptrTime = AllocVec(LEN_DATSTRING, MEMF_PUBLIC|MEMF_CLEAR)) == NULL) {close(prjWin) ; return(NULL) ;}
  119.   if ((dt =      AllocVec(sizeof(struct DateTime),  MEMF_PUBLIC)) == NULL) {close(prjWin) ; return(NULL) ;}
  120.   return prjWin ;
  121. }
  122.  
  123. void close(idWin * prj) {
  124.   int i;
  125.   if (prj) {                              // struct prj existe (<>0) 
  126.     endNotify(prj) ;
  127.     if (prj->win) CloseWindow(prj->win) ; // ferme la fenetre
  128.     free_twin(prj) ; 
  129.     if (prj->date) FreeVec(prj->date) ;   // libere datestamp 
  130.     if (prj->Notify) FreeVec(prj->Notify) ; // libere struct IntNotify
  131.     closeTimer(prj) ;
  132.     
  133.     if (NotifyIntuitionBase) CloseLibrary(NotifyIntuitionBase) ;
  134.     prj->wb = LockPubScreen("Workbench") ;
  135.     for(i = 0; i<NUM_COLORS; i++)
  136.       freePen(prj, prj->backWin.cmap[i].reg) ;
  137.     if (prj->wb) UnlockPubScreen(NULL, prj->wb) ;
  138.     FreeVec(prj) ;                        // libere la memoire
  139.     if (ptrTime) FreeVec(ptrTime) ;
  140.     free_bitmap(prj) ;
  141.   }
  142. }
  143. void getDate(idWin * prj) {
  144.   // remplit la structure (projet) avec la date
  145.    
  146.   DateStamp(prj->date) ;
  147.   if (ptrTime) {  
  148.     CopyMem(prj->date, &(dt->dat_Stamp), sizeof(struct DateStamp)) ;
  149.     dt->dat_Format = FORMAT_DOS ;
  150.     dt->dat_Flags = NULL ;
  151.     dt->dat_StrDay  = NULL ;
  152.     dt->dat_StrDate = NULL ;
  153.     dt->dat_StrTime = ptrTime ;
  154.     DateToStr(dt) ;
  155.     //CopyMem(ptrTime, prj->datestr, LEN_DATSTRING) ;
  156.     prj->oldH = prj->heu ;
  157.     prj->oldM = prj->min ;
  158.     prj->oldS = prj->sec ;
  159.     DateToByte(ptrTime, prj) ;
  160.   
  161.   }
  162. }
  163.  
  164. void processwin(idWin* prj) {
  165.   /* variables
  166.    */
  167.   ULONG signals = NULL,                                                                          /* masque des signaux a attendre */
  168.         mask,
  169.         class,                                                                                                                   /* masque des signaux recus */
  170.         sigtimer ;
  171.         
  172.   BOOL  stop    = FALSE ;                                                                                                                /* vrai si ctrl c */
  173.   struct IntuiMessage * msg = NULL ;                     /* ptr sur message intuition */
  174.   struct MsgPort      * extPort = NULL ;
  175.   struct backMsg      * extmsg = NULL ;
  176.   
  177.   extPort = CreateMsgPort() ;
  178.   extPort->mp_Node.ln_Name = AllocVec(10, MEMF_PUBLIC) ;
  179.   CopyMem("backclock", extPort->mp_Node.ln_Name, 10) ;
  180.   AddPort(extPort) ;
  181.   
  182.   sigtimer =  (1<<(prj->treq->tr_node.io_Message.mn_ReplyPort->mp_SigBit)) ; 
  183.   /* signaux d'attentes
  184.    */
  185.   signals = SIGBREAKF_CTRL_C | 
  186.             (1<<(prj->notifyPort->mp_SigBit)) | 
  187.             (1<<(prj->win->UserPort->mp_SigBit)) | sigtimer|
  188.             (1<<(extPort->mp_SigBit)) ;
  189.   
  190.   runtimer(prj) ;
  191.   getDate(prj) ;
  192.   retracer(prj) ;
  193.   while(!stop) {
  194.     mask = Wait(signals) ;
  195.     if (mask & SIGBREAKF_CTRL_C) {
  196.       /* recu le signal CTRL C
  197.        */
  198.       stop=TRUE;
  199.     }
  200.     if (mask & (1<<(extPort->mp_SigBit))) {
  201.       // recu message prog externe
  202.       extmsg = (struct backMsg *)GetMsg(extPort) ;
  203.       if (extmsg->Class == BC_Quit) {
  204.         // recu demande d'arret
  205.         stop=TRUE ;
  206.         extmsg->error = OK ;
  207.       }
  208.       if (extmsg->Class == BC_GetPrj) {
  209.         // recu demande d'envoi du projet
  210.         extmsg->ptrPrj = prj ;
  211.         extmsg->error = OK ;
  212.       }
  213.       if (extmsg->Class == BC_RefreshColors) {
  214.         setColors(prj) ;
  215.       }
  216.       if (extmsg->Class == BC_Refresh) {
  217.         // refresh window (redraw)
  218.         reinit_win(prj) ;
  219.         getDate(prj) ;
  220.         effacer(prj) ;
  221.         retracer(prj) ;
  222.         extmsg->error = OK ;
  223.       }
  224.       if (extmsg->Class == BC_SaveConf) {
  225.         SaveEnv(prj, TRUE) ;
  226.         if (prj->lastError) extmsg->error = prj->lastError ;
  227.       }
  228.       if (extmsg->Class == BC_UseConf) {
  229.         SaveEnv(prj, FALSE) ;
  230.         if (prj->lastError) extmsg->error = prj->lastError ;
  231.       }
  232.       if (extmsg->Class == BC_SetWindow) {
  233.         setWindow(prj) ;
  234.         extmsg->error = OK ;
  235.       }
  236.       if (extmsg) ReplyMsg((struct Message*)extmsg) ;
  237.     }
  238.     if (mask & (1<<(prj->notifyPort->mp_SigBit))) {
  239.       // attention fermeture du WB
  240.       if (partialClose(prj)) partialOpen(prj) ;
  241.     }
  242.     if (mask & 1<<(prj->win->UserPort->mp_SigBit)) {
  243.       /* recu message d'intuition
  244.        */
  245.       while((msg = GT_GetIMsg(prj->win->UserPort)) != NULL) {
  246.         /* retourne le message
  247.          */
  248.         class = msg->Class ;
  249.         ReplyMsg((struct Message*)msg) ;
  250.         if(class == IDCMP_CHANGEWINDOW) {
  251.           
  252.           RemoveGList(prj->win, &szgdg, 2) ;
  253.           szgdg.Width = prj->win->Width ;
  254.           tagdg.LeftEdge = prj->win->Width -10 ;
  255.           tagdg.TopEdge = prj->win->Height -10 ;
  256.           prj->backWin.width = prj->win->Width ;
  257.           prj->backWin.height = prj->win->Height ;      
  258.           prj->backWin.posX = prj->win->LeftEdge ;
  259.           prj->backWin.posY = prj->win->TopEdge ;
  260.           setWindow(prj) ;
  261.           reinit_win(prj) ; 
  262.           AddGList(prj->win, &szgdg, 0, 2, NULL) ;
  263.           RefreshGList(&szgdg, prj->win, NULL, -1) ;
  264.  
  265.           SaveEnv(prj, TRUE) ;
  266.         }  
  267.       }          
  268.     }
  269.     if (CheckIO((struct IORequest*)prj->treq)) {
  270.       WaitIO((struct IORequest*)prj->treq) ;
  271.       AbortIO((struct IORequest*)prj->treq) ;
  272.       WaitIO((struct IORequest*)prj->treq) ;
  273.       runtimer(prj) ;
  274.       getDate(prj) ;
  275.       effacer(prj) ;
  276.       retracer(prj) ;
  277.       //WindowToBack(prj->win) ;
  278.     }  
  279.  
  280.   }
  281.   AbortIO((struct IORequest*)prj->treq) ;
  282.   WaitIO((struct IORequest*)prj->treq) ;
  283.   RemPort(extPort) ;
  284.   FreeVec(extPort->mp_Node.ln_Name) ;
  285.   DeleteMsgPort(extPort) ;
  286.      
  287. }
  288.  
  289. void ez_req(UBYTE * title, UBYTE * body, UBYTE * gadg, APTR arglist ) {
  290.   struct EasyStruct * ez_rq ;
  291.   
  292.   if (ez_rq = AllocVec(sizeof(struct EasyStruct), MEMF_PUBLIC)) {
  293.     if (ez_rq->es_Title = AllocVec(strlen(title)+1, MEMF_PUBLIC)) {
  294.      strcpy(ez_rq->es_Title, title) ;
  295.      if (ez_rq->es_TextFormat = AllocVec(strlen(body)+1, MEMF_PUBLIC)) {
  296.       strcpy(ez_rq->es_TextFormat, body) ;
  297.       if (ez_rq->es_GadgetFormat = AllocVec(strlen(gadg)+1, MEMF_PUBLIC)) {
  298.        strcpy(ez_rq->es_GadgetFormat, gadg) ;
  299.        ez_rq->es_StructSize = sizeof(EasyStruct) ;
  300.        ez_rq->es_Flags = NULL ;
  301.        EasyRequestArgs(NULL, ez_rq, NULL, arglist) ;
  302.        FreeVec(ez_rq->es_GadgetFormat) ;
  303.       }
  304.       FreeVec(ez_rq->es_TextFormat) ;
  305.      }
  306.      FreeVec(ez_rq->es_Title) ;
  307.     }
  308.     FreeVec(ez_rq) ;
  309.   }
  310. }
  311.  
  312.